//
//  TraceTableViewCell.m
//  bondwatch_purecode
//
//  Created by Jason Yuan on 10/27/15.
//  Copyright © 2015 Sumridge. All rights reserved.
//

#import "TraceTableViewCell.h"

@implementation TraceTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        traceLabel = [[UILabel alloc] init];
        traceLabel.font = ARIAL10;
        priceQtyLabel = [[UILabel alloc] init];
        priceQtyLabel.font = ARIAL10;
        yieldLabel = [[UILabel alloc] init];
        yieldLabel.textAlignment = NSTextAlignmentRight;
        yieldLabel.font = ARIAL10;
        spreadLabel = [[UILabel alloc] init];
        spreadLabel.textAlignment = NSTextAlignmentRight;
        spreadLabel.font = ARIAL10;
        
        [self addSubview:traceLabel];
        [self addSubview:priceQtyLabel];
        [self addSubview:yieldLabel];
        [self addSubview:spreadLabel];
    }
    return self;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setUIFrame {
    if ([UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationLandscapeLeft ||
        [UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationLandscapeRight) {
        
    } else {
        [traceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(105);
        }];
        
        [priceQtyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(80);
        }];
        
        [spreadLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(50);
        }];
        
        [yieldLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(spreadLabel.mas_left).offset(-5);
            make.centerY.equalTo(self);
            make.width.mas_equalTo(50);
        }];
    }
}

- (void)setLabelText:(NSDictionary *)traceData {
    NSString *tradeDate = [traceData objectForKey:@"tradeDate"];
    NSString *tradeTime = [traceData objectForKey:@"tradeTime"];
    [traceLabel setText:[NSString stringWithFormat:@"%@ %@", tradeDate, tradeTime]];
    
    NSString *tradeType = [traceData objectForKey:@"tradeType"];
    if ([tradeType isEqualToString:@"Customer Sell"]) {
        [priceQtyLabel setTextColor:[UIColor redColor]];
    } else {
        [priceQtyLabel setTextColor:[UIColor colorWithRed:102.0/255.0 green:168.0/255.0 blue:45.0/255.0 alpha:1.0]];
    }
    
    NSNumber *quantity = [traceData objectForKey:@"quantity"];
    NSNumber *price = [traceData objectForKey:@"price"];
    [priceQtyLabel setText:[NSString stringWithFormat:@"%@M@%.3f", quantity, [price floatValue]]];
    
    NSNumber *yield = [traceData objectForKey:@"yield"];
    [yieldLabel setText:[NSString stringWithFormat:@"%.3f", [yield floatValue]]];
    
    NSNumber *spread = [traceData objectForKey:@"spread"];
    [spreadLabel setText:[NSString stringWithFormat:@"%.1f", [spread floatValue]]];
}

@end
